var passwordRecoveryNewEnv = {
    // LIMITS and ERRORS defined in register.html.
    LIMITS: LIMITS,
    MESSAGES: MESSAGES,
    
    // errors global variable defined in register.html.
    server_validation_errors: errors,
    
    validator: null,
    
    // If page is currently in the middle of processing a form submit.
    busySubmitting: false
}


function createClientSideValidation() {
    var limits = passwordRecoveryNewEnv.LIMITS;
    var messages = passwordRecoveryNewEnv.MESSAGES;
    
    // Method validating if value consists only of word characters.
    $.validator.addMethod("word", function(value, element) {
        var re = new RegExp(/^\w*$/);
        return re.test(value);
    });
    
    passwordRecoveryNewEnv.validator = $("#window form").validate({
        rules: {
            "password": {
                required:    true,
                rangelength: [limits["password_min_length"],
                              limits["password_max_length"]],
                word:        true
            },
            "password-repeat": {
                required: true,
                equalTo:  "#password"
            }
        },
        
        messages: {
            "password": {
                required:    messages["password_not_defined"],
                rangelength: messages["password_invalid_length"],
                word:        messages["password_invalid_format"]
            },
            "password-repeat": {
                required: messages["password_repeat_not_defined"],
                equalTo:  messages["password_repeat_equal_to"]
            },
        },
        
        errorClass: "error"
    })
}


function showServerValidationErrorMessages(errors) {
    var messages = passwordRecoveryNewEnv.MESSAGES;
    var errorsToShow = { };
    
    if (errors["password_not_defined"])
        errorsToShow["password"] = messages["password_not_defined"];
    if (errors["password_invalid_length"])
        errorsToShow["password"] = messages["password_invalid_length"];
    if (errors["password_invalid_format"])
        errorsToShow["password"] = messages["password_invalid_format"];
    
    passwordRecoveryNewEnv.validator.showErrors(errorsToShow);
}


function disableMultipleFormSubmission() {
    $("#window form").submit(function() {
        // We must check the form's validation, because submit event is
        // triggerred before validation. And so so we don't want to disable
        // submit button if the form is invalid, because the submission
        // will be cancelled anyway.
        if (passwordRecoveryNewEnv.validator.form()) {
            $(this).unbind("submit").submit(function() {
                return false;
            });
            
            $("#submit").val("Wait...");
        }
        else {
            return false;
        }
    });
}


$(document).ready(function() {
    createClientSideValidation();
    showServerValidationErrorMessages(passwordRecoveryNewEnv.server_validation_errors);
    disableMultipleFormSubmission();
})
